package io.github.veigara.tcp.controller;

import io.github.veigara.tcp.framework.service.DeviceConnectionService;
import io.github.veigara.tcp.framework.service.DeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Map;

@RestController
@RequestMapping("/api/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceConnectionService deviceConnService;
    /**
     * 接收远程服务器转发的设备消息
     */
    @PostMapping("/message/{deviceId}")
    public ResponseEntity<Void> handleDeviceMessage(@PathVariable String deviceId, @RequestBody Map<String, String> messageData) {
        String message = messageData.get("message");
        boolean success=deviceService.sendMessage(deviceId, message);

        if (!success) {
            return ResponseEntity.notFound().build();
        }
        
        return ResponseEntity.ok().build();
    }

//    @GetMapping("/online/devices")
//    public ResponseEntity<List<DeviceConnection>> onlineDevice() {
//        List<DeviceConnection> connections = deviceConnService.getAllDeviceConnections();
//
//        return ResponseEntity.ok(connections);
//    }
}    